<?php
/**
 * this is cloud search http request class test file
 *
 *
 */
require_once 'index.php';
require_once 'Framework/TestCase.php';
require_once(DIR_LIBRARY."orm".DS."ctp".DS."thread.php");
require_once(DIR_LIBRARY."orm".DS."ctp".DS."post.php");
class CtpTest extends PHPUnit_Framework_TestCase 
{
    private static $config = array(
        "user_id" => 1,
        "type"    => 1,        
        "app"     => CtpThread::APP_NICE,
    );

    public function testCreateThread()
    {
        $user_id = self::$config["user_id"];
        $type    = self::$config["type"];
        $app     = self::$config["app"];

        $thread = $this->createThread($app, $user_id, $type);
        $this->assertTrue((bool)$thread->getId());

        $thread = $this->getThread($app, $thread->getSequence());
        $this->assertTrue((bool)$thread->getId());
        $this->assertTrue($thread->getApp() == $app);
        $this->assertTrue($thread->getType() == $type);
        $this->assertTrue($thread->getCreator() == $user_id);
        $this->assertTrue($thread->getStatus() == CtpThread::STATUS_AVAILABLE);
        $this->assertTrue($thread->getCreated() != 0);
        $this->assertTrue($thread->getUpdated() != 0);
    }

    private function createThread($app, $user_id, $type)
    {
        $thread = CtpThread::factory();
        $thread->setApp($app);
        $thread->setType($type);
        $thread->setCreator($user_id);
        $thread->create();

        return $thread;
    }

    private function getThread($app, $sequence)
    {
        $thread = CtpThread::factory();
        $thread->getThread($app, $sequence);

        return $thread;
    }

    public function testUpdateThread()
    {
        $user_id = self::$config["user_id"];
        $type    = self::$config["type"];
        $app     = self::$config["app"];

        $status = 100;

        $thread = $this->createThread($app, $user_id, $app);
        $bool   = $this->updateThread($app, $thread->getSequence(), $status);
        $this->assertTrue($bool);

        $thread = $this->getThread($app, $thread->getSequence());
        $this->assertTrue($thread->getStatus() == $status);
    }

    private function updateThread($app, $sequence, $status)
    {
        $thread = CtpThread::factory();
        $thread->setStatus($status);
        $bool = $thread->update($app, $sequence);

        return $bool;
    }

    public function testDeleteThread()
    {
        $user_id = self::$config["user_id"];
        $type    = self::$config["type"];
        $app     = self::$config["app"];

        $thread = $this->createThread($app, $user_id, $app);

        $bool = $this->deleteThread($app, $thread->getSequence());
        $this->assertTrue($bool);

        $thread = $this->getThread($app, $thread->getSequence());
        $this->assertTrue(CtpThread::STATUS_DELETED == $thread->getStatus());
    }

    private function deleteThread($app, $sequence)
    {
        $thread = CtpThread::factory();
        $bool = $thread->delete($app, $sequence);

        return $bool;
    }

    public function testCreatePost($data = NULL)
    {
        $user_id = self::$config["user_id"];
        $type    = self::$config["type"];
        $app     = self::$config["app"];

        if (!$data)
        {
            $meta = array("image_url" => "http://media-cache-ec2.pinterest.com/upload/102597697731710876_kiVK8hve_b.jpg", "image_height" => 256, "body" => "花姑娘地有，0");
        }
        else
        {
            $meta = $data;
        }

        //create thread
        $thread = $this->createThread($app, $user_id, $app);
        $post   = $this->createPost($thread, $user_id, $meta);

        $this->assertTrue((bool)$post->getId());
        $this->assertTrue((bool)$thread->getId());

        $thread = $this->getThread($app, $thread->getSequence());
        $this->assertTrue((bool)$thread->getId());

        $post = $this->getPost($thread, $post->getSequence());
        $this->assertTrue((bool)$post->getId());
        $this->assertTrue($post->getParent() == $thread->getId());
        $this->assertTrue($post->getBody() == $meta["body"]);
        $this->assertTrue($post->getStatus() == CtpPost::STATUS_AVAILABLE);
        $this->assertTrue($post->getCreator() == $user_id);
        $this->assertTrue($post->getCreated() != 0);
        $this->assertTrue($post->getUpdated() != 0);
    }

    private function createPost($thread, $user_id, $meta)
    {
        //create post
        $post  = CtpPost::factory();
        $post->setBody($meta["body"]);
        $post->setCreator($user_id);
        $post->setMeta(serialize($meta));
        $bool = $post->create($thread);

        return $post;
    }

    private function getPost($thread, $sequence)
    {
        $post  = CtpPost::factory();

        return $post->getPost($thread, $sequence);
    }


    public function testUpdatePost()
    {
        $user_id = self::$config["user_id"];
        $type    = self::$config["type"];
        $app     = self::$config["app"];

        $meta = array("body" => "花姑娘地有", "image_height" => 100);

        $thread = $this->createThread($app, $user_id, $app);
        $post   = $this->createPost($thread, $user_id, $meta);
        $meta   = array("body" => "abcdefg");

        $bool = $this->updatePost($thread, $post->getSequence(), $meta);

        $this->assertTrue($bool);

        $post = $this->getPost($thread, $post->getSequence());
        $this->assertTrue($post->getBody() == $meta["body"]);
    }


    private function updatePost($thread, $sequence, $meta)
    {
        $post = CtpPost::factory();
        $post->setBody($meta["body"]);
        $post->setMeta(serialize($meta));
        $bool = $post->update($thread, $sequence);

        return $bool;
    }

    public function testDeletePost()
    {
        $user_id = self::$config["user_id"];
        $type    = self::$config["type"];
        $app     = self::$config["app"];

        $meta = array("body" => "花姑娘地有", "image_height" => 100);

        $thread = $this->createThread($app, $user_id, $app);
        $post   = $this->createPost($thread, $user_id, $meta);
        $meta   = array("body" => "abcdefg");

        $bool = $this->deleteThread($thread->getApp(), $thread->getSequence());
        $this->assertTrue($bool);
        $thread = $this->getThread($thread->getApp(), $thread->getSequence());
        $this->assertTrue(CtpThread::STATUS_DELETED == $thread->getStatus());

        $bool = $this->deletePost($thread, $post->getSequence());
        $this->assertTrue($bool);
        $post = $this->getPost($thread, $post->getSequence());
        $this->assertTrue(CtpPost::STATUS_DELETED == $post->getStatus());
    }

    private function deletePost($thread, $sequence)
    {
        $post = CtpPost::factory();
        $bool = $post->delete($thread, $sequence);

        return $bool;
    }

    private static function truncateTable()
    {
        $thread = CtpThread::factory();
        $thread->setApp(CtpThread::APP_NICE);
        $table_name = $thread->rebuildTableName();
        $sql = "truncate table ".$table_name;
        $rs = $thread->query($sql);
        
        if (!$rs->isSuccess())
        {
            throw new Exception("query fail");
        }
        echo "truncate table ".$table_name."\r\n";

        $post = CtpPost::factory();
        $post->setApp(CtpThread::APP_NICE);
        $post->setShard($thread->generateShard(CtpThread::APP_NICE, time()));
        $table_name = $post->rebuildTableName();
        $sql = "truncate table ".$table_name;
        $rs = $thread->query($sql);
        
        if (!$rs->isSuccess())
        {
            throw new Exception("query fail");
        }
        echo "truncate table ".$table_name."\r\n";
    }

    public function __testCreatePosts()
    {
        $rows = $this->getThreadData();

        foreach ($rows as $row)
        {
            $this->testCreatePost($row);
        }
    }

    private function getThreadData()
    {
        $data = array();
        $file_name = "data.txt";
        $str = file_get_contents($file_name);
        $slice = explode("bbbb", $str);

        $i = 0;
        foreach ($slice as $s)
        {
            $row = explode("aaaa", $s);
            if (count($row) < 2)
            {
                echo "row count less than 2\n";
                continue;
            }

            $data[$i]["body"] = addslashes($row[0]);
            $data[$i]["image_url"] = $row[1];
            $size = getimagesize($row[1]);
            if (false === $size)
            {
                echo "get imagesize fail\n";
                continue;
            }
            $data[$i]["image_height"] = $size[1];
            $i++;
        }

        return $data;
    }

    /**
     * execute before class
     *
     */
    public static function setUpBeforeClass()
    {
        self::truncateTable();
    }


    /**
     * execute before case
     *
     */
    protected function setUp()
    {

    }

    /**
     *
     * execute before assert
     */
    protected function assertPreConditions()
    {

    }

    /**
     *
     * execute after assert
     */
    protected function assertPostConditions()
    {

    }

    /**
     * execute after case
     */
    protected function tearDown()
    {

    }

    /**
     * execute after class
     *
     */
    public static function tearDownAfterClass()
    {
        self::truncateTable();
    }

}//end class
